package com.manong.bookstore.shoppingcart.controller;

import java.util.*;

import com.manong.bookstore.shoppingcart.feign.ProductFeignService;
import com.manong.bookstore.shoppingcart.vo.SCartProduceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.manong.bookstore.shoppingcart.entity.ShoppingCartEntity;
import com.manong.bookstore.shoppingcart.service.ShoppingCartService;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * 
 *
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-27  21:29:25
 */
@Slf4j
@RestController
@RequestMapping("shoppingcart/shoppingcart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private ProductFeignService feignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = shoppingCartService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 弃用
     * @param userid
     * @param session
     * @return
     */
    @RequestMapping("/settingid")
    public R settingIdIntoSession(@RequestBody Integer userid,HttpSession session){
        session.setAttribute("userId",userid);
        return R.ok();
    }

    @RequestMapping("/cart")
    public R listShoppingCart(HttpSession session){

        //TODO 从session中取出userid
        //Integer userID= (Integer) request.getSession().getAttribute("userID");
        Integer userID = (Integer) session.getAttribute("userId");
        //System.out.println(userID);
        //System.out.println("userID:"+userID);
        List<SCartProduceInfo> shoppingCart = shoppingCartService.ListUserShoppingCart(userID);
        for (SCartProduceInfo sCartProduceInfo:shoppingCart) {
            sCartProduceInfo.setProduceStock((Integer) (((Map<String, Object>) (feignService.info(sCartProduceInfo.getProduceId()).get("produce"))).get("produceStock")));
        }
        return R.ok().put("cart", shoppingCart);
    }

    @RequestMapping("/sum")
    public R userCartSum(HttpSession session){
        Integer userID = (Integer) session.getAttribute("userId");
        List<SCartProduceInfo> shoppingCart = shoppingCartService.ListUserShoppingCart(userID);
        Integer cartSum=0;
        for (SCartProduceInfo sCartProduceInfo:shoppingCart) {
            cartSum+=sCartProduceInfo.getProduceQuantity();
        }
        return R.ok().put("cartSum", cartSum);
    }

    /**
     * 订单确认时的商品获取
     * @return
     */
    @RequestMapping("/select/info")
    public R selectInfo(@RequestBody Integer[] shoppingCartIds,HttpSession session){
        Integer userId = (Integer) session.getAttribute("userId");
        //Integer[] select= (Integer[]) session.getAttribute(session.getAttribute("userId")+"select");
        List<ShoppingCartEntity> shoppingCartEntities=shoppingCartService.listSelectProduce(userId,shoppingCartIds);
        return R.ok().put("select", shoppingCartEntities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{shoppingCartId}")
    public R info(@PathVariable("shoppingCartId") Integer shoppingCartId){
		ShoppingCartEntity shoppingCart = shoppingCartService.getById(shoppingCartId);
        return R.ok().put("shoppingCart", shoppingCart);
    }

    /**
     * 用户发送商品信息
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ShoppingCartEntity shoppingCart, HttpSession session){
        ShoppingCartEntity shoppingCartEntity=shoppingCartService.ProduceExistOrNot(shoppingCart.getUserId(),shoppingCart.getProduceId());
        //System.out.println("entity:"+shoppingCartEntity);
        if (shoppingCartEntity==null) {
            shoppingCartService.save(shoppingCart);

        }
        else {
            shoppingCart.setProduceQuantity(shoppingCart.getProduceQuantity()+shoppingCartEntity.getProduceQuantity());
            update(shoppingCart);
            shoppingCart.setShoppingCartId(shoppingCartEntity.getShoppingCartId());
        }
        //System.out.println(shoppingCart);
        return R.ok().put("shoppintCartID",shoppingCart.getShoppingCartId());
    }

    public R update(ShoppingCartEntity shoppingCart){
        shoppingCartService.updateByProduceId(shoppingCart, shoppingCart.getUserId());
        return R.ok();
    }


    @RequestMapping("/update")
    public R updateNum(@RequestBody ShoppingCartEntity shoppingCart,HttpSession session){
        System.out.println(session.getAttribute("userId"));
        shoppingCartService.updateByProduceId(shoppingCart, (Integer) session.getAttribute("userId"));
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] shoppingCartIds,HttpSession session){
        //shoppingCartService.removeByIds(Arrays.asList(shoppingCartIds));
        //TODO 从session中获取用户ID
        Integer userId = (Integer) session.getAttribute("userId");
        shoppingCartService.removeByProductId(userId,shoppingCartIds);
        return R.ok();
    }
    @RequestMapping("/deleteByFeign")
    public void deleteByFeign(@RequestParam Integer[] shoppingCartIds,@RequestParam Integer UserId){
        shoppingCartService.removeByProductId(UserId,shoppingCartIds);
    }

    /**
     * 删除
     */
    /*@RequestMapping("/deleteAll")
    public R deleteAll(@RequestBody Integer[] shoppingCartIds){
		shoppingCartService.removeByIds(Arrays.asList(shoppingCartIds));
        return R.ok();
    }*/

}
